<?php

/**
 * This is the model class for table "{{kelurahan}}".
 *
 * The followings are the available columns in table '{{kelurahan}}':
 * @property integer $id_kelurahan
 * @property string $nama_kelurahan
 * @property integer $id_kecamatan
 *
 * The followings are the available model relations:
 * @property Desa[] $desas
 * @property Kecamatan $idKecamatan
 * @property KelurahanAnggota[] $kelurahanAnggotas
 */
class Kelurahan extends CActiveRecord
{
    public $nama_kecamatan;
    public $nama_kabupaten;
    public $id_kabupaten;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Kelurahan the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{kelurahan}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nama_kelurahan, id_kecamatan', 'required'),
			array('id_kecamatan', 'numerical', 'integerOnly'=>true),
			array('nama_kelurahan', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_kelurahan, nama_kelurahan, id_kecamatan,nama_kecamatan,nama_kabupaten,id_kabupaten', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'desas' => array(self::HAS_MANY, 'Desa', 'id_kelurahan'),
			'idKecamatan' => array(self::BELONGS_TO, 'Kecamatan', 'id_kecamatan'),
			'kelurahanAnggotas' => array(self::HAS_MANY, 'KelurahanAnggota', 'id_kelurahan'),
            'idKabupaten' => array(self::HAS_ONE, 'Kabupaten', 'id_kabupaten','through'=>'idKecamatan'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_kelurahan' => 'Id Kelurahan',
			'nama_kelurahan' => 'Nama Kelurahan',
			'id_kecamatan' => 'Id Kecamatan',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_kelurahan',$this->id_kelurahan);
		$criteria->compare('nama_kelurahan',$this->nama_kelurahan,true);
		$criteria->compare('id_kecamatan',$this->id_kecamatan);

//        $criteria->join = 'inner JOIN psn_kecamatan idKecamatan ON idKecamatan.id_kecamatan=t.id_kecamatan
//                           inner JOIN psn_kabupaten idKabupaten ON idKabupaten.id_kabupaten=idKecamatan.id_kabupaten';
        $criteria->with = array('idKecamatan','idKabupaten');
        $criteria->compare('idKecamatan.nama_kecamatan', $this->nama_kecamatan, true);
        $criteria->compare('idKecamatan.id_kabupaten', $this->id_kabupaten, true);
        $criteria->compare('idKabupaten.nama_kabupaten', $this->nama_kabupaten, true);

        $criteria->together=true;
                
        return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}